package edu.pitt.dbmi.odie.gapp.gwt.server.navigator;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.allen_sauer.gwt.log.client.Log;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import edu.pitt.dbmi.odie.gapp.gwt.server.util.rest.ODIE_NcboRestUtils;

@SuppressWarnings("serial")
public class ODIE_NavigatorSnapShotServlet extends HttpServlet {

	private ServletConfig config = null;

	public void init(ServletConfig config) {
		this.config = config;
	}

	// analysis?category=analysis&action=list - pulls Analysis list xml
	// analysis?title=<title> - pulls Documents list xml for

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
//		UserService userService = UserServiceFactory.getUserService();
//		User user = userService.getCurrentUser();
		ODIE_SelectionCacher odieSelectionCacher = null;
		StringBuffer sb = new StringBuffer();
		sb.append(ODIE_NcboRestUtils.getXmlHeader());
		odieSelectionCacher = ODIE_SelectionCacher
				.fetchOdieSelectionCacher(config);
		if (req.getParameter("category") == null || odieSelectionCacher == null) {
			sb.append(ODIE_NcboRestUtils.emptyPayLoad);
		} else if (req.getParameter("category").equals("focus")) {
			sb.append(ODIE_NcboRestUtils.payLoadStartElement);
			sb.append(odieSelectionCacher.fetchAnalysisXml());
			sb.append(ODIE_NcboRestUtils.payLoadEndElement);
		} else if (req.getParameter("category").equals("analysis")) {
			sb.append(ODIE_NcboRestUtils.payLoadStartElement);
			sb.append(odieSelectionCacher.fetchAnalyses());
			sb.append(ODIE_NcboRestUtils.payLoadEndElement);
		} else if (req.getParameter("category").equals("document")) {
			sb.append(ODIE_NcboRestUtils.payLoadStartElement);
			sb.append(odieSelectionCacher.fetchDocuments());
			sb.append(ODIE_NcboRestUtils.payLoadEndElement);
		} else if (req.getParameter("category").equals("annotation")) {
			sb.append(ODIE_NcboRestUtils.payLoadStartElement);
			sb.append(odieSelectionCacher.fetchAnnotations());
			sb.append(ODIE_NcboRestUtils.payLoadEndElement);
		} else if (req.getParameter("category").equals("cls")) {
			if (req.getParameter("id") != null) {
				// This is an update
				String odieId = req.getParameter("id") ;
				String color = req.getParameter("color") ;
				odieSelectionCacher.updateCls(odieId, color) ;
			}
			sb.append(ODIE_NcboRestUtils.payLoadStartElement);
			sb.append(odieSelectionCacher.fetchClses());
			sb.append(ODIE_NcboRestUtils.payLoadEndElement);
		}
		Log.debug(sb.toString());
		resp.getWriter().write(sb.toString());
		resp.getWriter().close();
	}
	
	@SuppressWarnings({ "unused", "unchecked" })
	private void displayParameters(HttpServletRequest req) {
		for (Enumeration<String> parametersEnumeration = req.getParameterNames() ; parametersEnumeration.hasMoreElements() ;) {
			String parameterName = parametersEnumeration.nextElement() ;
			String parameterValue = req.getParameter(parameterName) ;
			Log.debug(parameterName + " ==> " + parameterValue) ;
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		doGet(req, resp) ;
	}

}
